package top.smallway.bs_vr;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import top.smallway.bs_vr.recycelviewAdapeter.SongAdapeter;
import top.smallway.bs_vr.requestsBS.Main;
import top.smallway.bs_vr.requestsBS.Song;


public class Search_Song extends AppCompatActivity {
    private EditText song_name_edit;
    private String name;
    private Button search;
    private AlertDialog dialog;
    private CheckBox checkBox;
    private List<Song> data = new ArrayList<>();
    private RecyclerView recyclerView;
    private TextView about, arrange, title;
    private GridLayoutManager gridLayoutManager;
    private SongAdapeter songAdapeter;
    private int page = 0;
    private int Static=1;
    private final Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void dispatchMessage(@NonNull Message msg) {
            super.dispatchMessage(msg);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                dialog.dismiss();
                songAdapeter.updateData((List<Song>) msg.obj);


            } else if (msg.what == 0) {
                dialog.dismiss();
                songAdapeter.setLoadMoreEnabled(false);
                Toast.makeText(Search_Song.this, "搜索出现未知异常", Toast.LENGTH_SHORT).show();
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_song);
        initview();
        checkbox();
        title.setText(Html.fromHtml("<b>光团助手</b>"));

        arrange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Search_Song.this, Arrange.class);
                startActivity(intent);
            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Search_Song.this, About.class);
                startActivity(intent);
            }
        });
        gridLayoutManager = new GridLayoutManager(this, 5);
        recyclerView.setLayoutManager(gridLayoutManager);
        songAdapeter=new SongAdapeter(data,this);
        recyclerView.setAdapter(songAdapeter);
        // 添加滚动监听器
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // 获取最后可见的项的位置
                int lastVisibleItemPosition = gridLayoutManager.findLastVisibleItemPosition();
                // 判断是否满足触发加载更多的条件
                if (lastVisibleItemPosition == songAdapeter.getItemCount() - 1) {
                    // 执行加载更多的操作，例如从服务器请求数据
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Message message = new Message();
                            try {
                                List<Song> Mdata = Main.search(song_name_edit.getText().toString(), page++);
                                Log.i("页数", String.valueOf(page));
                                if (Mdata.isEmpty()) {
                                    message.what = 0;
                                } else {
                                    message.obj=Mdata;
                                    message.what = 1;
                                }
                            } catch (IOException e) {
                                message.what = 0;
                            }
                            mHandler.sendMessage(message);

                        }
                    }).start();
                    // 更新适配器的数据集


                    // 隐藏FooterView
                    songAdapeter.setLoadMoreEnabled(false);
                }
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                songAdapeter.clearData();
                if (Static==0){
                    name="!bsr "+song_name_edit.getText().toString();
                }else name=song_name_edit.getText().toString();

                Log.i("搜索名称", name);
                if (dialog == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Search_Song.this);

                    builder.setMessage("正在搜索中......");
                    dialog = builder.create();
                    dialog.setCancelable(false);
                    dialog.show();
                } else {
                    dialog.show();
                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        try {
                            data = Main.search(name, page);
                            message.obj=data;
                            Log.d("请求返回值", String.valueOf(data));
                            message.what = 1;
                        } catch (IOException e) {
                            message.what = 0;
                        }
                        mHandler.sendMessage(message);

                    }
                }).start();
            }
        });
    }

    private void checkbox() {
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    Static=0;
                } else {
                    Static=1;
                }
            }
        });
    }

    private void initview() {
        song_name_edit = findViewById(R.id.song_name_edit);
        search = findViewById(R.id.search);
        recyclerView = findViewById(R.id.Recycelview);
        about = findViewById(R.id.about);
        arrange = findViewById(R.id.arrange);
        title = findViewById(R.id.title);
        checkBox = findViewById(R.id.checkBox);
    }

}